<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/Abstract.php';

/**
 * Helper to generate info about file extension and size.
 * Example: (PDF, 2.9MB)
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3fileInfo extends Zend_View_Helper_Abstract
{

	/**
	 * Returns a string with info about file extension and size.
	 *
	 * @param string $relFilePath
	 * @return string
	 */
	public function t3fileInfo($relFilePath) {
		if(is_file($relFilePath)) {
			$parts = pathinfo($relFilePath);
			$filesizeString = '';
			$filesizenames = array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
			$filesize = filesize($relFilePath);
			if($filesize > 0) {
				$i = floor(log($filesize, 1024));
				$exponentialValue = pow(1024, ($i));
				$decimals = min(1, $i);
				$filesizeString = round($filesize/$exponentialValue, $decimals) . $filesizenames[$i];
			} else {
				$filesizeString = '0' . $filesizenames['0'];
			}
			if(empty($parts['extension'])) {
				return sprintf('<span class="fileinfo" style="white-space:nowrap">(%s)</span>', $filesizeString);
			} else {
				return sprintf('<span class="fileinfo" style="white-space:nowrap">(%s, %s)</span>', strtoupper($parts['extension']), $filesizeString);
			}
		}
	}

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3fileInfo.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3fileInfo.php']);
}
